#include <Timer.h>
#include <stm32f10x.h>
#include "can.h"
/*****************************************************
** @函数名：Timer4_init
** @描述  ：定时器4初始化
** @参数  ：None
** @返回值：None
*****************************************************/
void Timer4_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;                       //中断向量表配置结构
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);        //打开定时器4的时钟
	
	TIM_DeInit(TIM4);                                          //清空定时器
	//定时器相关配置 
  TIM_TimeBaseStructure.TIM_Period            = 199;         //自动装在周期，计数时间为20ms    
  TIM_TimeBaseStructure.TIM_Prescaler         = 7199;        //设置分频系数 
  TIM_TimeBaseStructure.TIM_ClockDivision     = TIM_CKD_DIV1;     
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;      
	TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Up;  //技术方式
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 
	
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);     
	TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);	
	
  NVIC_InitStructure.NVIC_IRQChannel                   = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
  TIM_Cmd(TIM4,ENABLE);                      //打开定时器	
}
/*****************************************************
** @函数名：Timer3_init
** @描述  ：定时器3初始化
** @参数  ：None
** @返回值：None
*****************************************************/
void Timer3_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;                       //中断向量表配置结构
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);        //打开定时器4的时钟
	
	TIM_DeInit(TIM4);                                          //清空定时器
	//定时器相关配置 
  TIM_TimeBaseStructure.TIM_Period            = 9999;         //自动装在周期，计数时间为1000ms    
  TIM_TimeBaseStructure.TIM_Prescaler         = 7199;        //设置分频系数 
  TIM_TimeBaseStructure.TIM_ClockDivision     = TIM_CKD_DIV1;     
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;      
	TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Up;  //技术方式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
	
  TIM_ClearFlag(TIM3, TIM_FLAG_Update);     
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);	
	
  NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
  TIM_Cmd(TIM3,ENABLE);                      //打开定时器	
}
/*****************************************************
** @函数名：TIM4_IRQHandler
** @描述  ：定时器4中断
** @参数  ：None
** @返回值：None
*****************************************************/
 void TIM4_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
	{
		
		Touch_Trigger_sys();                             //20ms调用一次触摸系统
		TIM_ClearITPendingBit(TIM4,TIM_IT_Update);       //清除标志位
	}		
}
/*****************************************************
** @函数名：TIM3_IRQHandler
** @描述  ：定时器3中断
** @参数  ：None
** @返回值：None
*****************************************************/
void TIM3_IRQHandler(void)
{
	uint8_t flag = 0;
	uint8_t can_send[8];
	if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
	{	
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);       //清除标志位
		  can_send[0] = (I_value_set>>0)&0xFF;
		  can_send[1] = (I_value_set>>8)&0xFF;
		  can_send[2] = (Obj_value_set>>0)&0xFF;         
		  can_send[3] = (Obj_value_set>>8)&0xFF;              		
		  //can_send[4] = JH_value_set;
		  can_send[4] = (Cell_type_set<<4)&0xF0;
		  can_send[4] |= JH_value_set & 0x0F;
		  
		  can_send[5] = (SW_value_set>>0)&0xFF;
		  can_send[6] = (SW_value_set>>8)&0xFF;
		  can_send[7] = (SW_value_set>>16)&0xFF;
		  Can_Send_Msg(0x1201B1F1,can_send,8);		
	}
}
